package bpm.dao.impl.document;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import bpm.domain.document.Attachment;
import bpm.domain.document.AttachmentOriginType;
import bpm.domain.document.AttachmentType;
import bpm.domain.document.Document;
import bpm.domain.document.AttachmentType.AttachmentTypeKey;
import bpm.domain.document.AttachmentType.PaperyType;
import bpm.domain.document.AttachmentType.ReferenceType;
import bpm.domain.user.OrgUser;
/**
 * 
 * @author ZhaoWangLong Jul 27, 2011
 *
 */
public class AttachmentRowMapper implements RowMapper {
	private boolean typeName;
	public AttachmentRowMapper(){}
	public AttachmentRowMapper(boolean typeName){
		this.typeName=typeName;
	}
	public Object mapRow(ResultSet rs, int i) throws SQLException {
		Attachment attch=new Attachment();
		attch.setId(rs.getString("ID"));
		AttachmentType type=new AttachmentType();
		type.setId(rs.getInt("TYPE_ID"));
		if(typeName){
			type.setName(rs.getString("TYPE_NAME"));
			String typeKey=rs.getString("TYPE_KEY");
			if(typeKey!=null&&!"".equals(typeKey))
			type.setKey(AttachmentTypeKey.valueOf(typeKey));
			type.setRank(rs.getInt("TYPE_RANK"));
			String typeReference=rs.getString("TYPE_REFERENCE");
			if(typeReference!=null&&!"".equals(typeReference))
			type.setReference(ReferenceType.valueOf(typeReference));
			String papery=rs.getString("TYPE_PAPERY");
			if(papery!=null&&!"".equals(papery))
			type.setPapery(PaperyType.valueOf(papery));
			type.setEnable("1".equals(rs.getString("TYPE_ENABLE").trim())?true:false);
			type.setCreateTime(rs.getDate("TYPE_CREATE_TIME"));
			type.setLastUpdateTime(rs.getDate("TYPE_LAST_UPDATE_TIME"));
			type.setDescription(rs.getString("TYPE_DESCRIPTION"));
		}
		attch.setType(type);
		AttachmentOriginType referencedResourceType=AttachmentOriginType.valueOf(rs.getString("RES_TYPE"));
		attch.setReferencedResourceType(referencedResourceType);
		attch.setReferencedResourceId(rs.getString("RES_ID"));
		Document ownedDocument =new Document();
		ownedDocument.setId(rs.getString("DOC_ID"));
		attch.setOwnedDocument(ownedDocument);
		attch.setPaper("1".equals(rs.getString("PAPER").trim())?true:false);
		attch.setTitle(rs.getString("TITLE"));
		attch.setSize(rs.getInt("SIZE"));
		OrgUser creator=new OrgUser();
		creator.setId(rs.getString("CREATOR_ID"));
		creator.setName(rs.getString("CREATOR_NAME"));
		attch.setCreator(creator);
		OrgUser lastUpdater=new OrgUser();
		lastUpdater.setId(rs.getString("LAST_UPDATER_ID"));
		lastUpdater.setName(rs.getString("LAST_UPDATER_NAME"));
		attch.setLastUpdater(lastUpdater);
		attch.setCreateTime(rs.getDate("CREATE_TIME"));
		attch.setLastUpdateTime(rs.getDate("LAST_UPDATE_TIME"));
		attch.setDescription(rs.getString("DESCRIPTION"));
		attch.setScope(rs.getString("SCOPE"));
		return attch;
	}

}
